<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import { listEmployee } from '@/api/employee'
import { FirstAidKit } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 表单数据
const loginForm = reactive({
  employeeName: '',
  password: ''
})

// 表单规则
const rules = {
  employeeName: [{ required: true, message: '请输入管理员账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

// 表单引用
const loginFormRef = ref(null)
// 加载状态
const loading = ref(false)

// 登录方法
const handleLogin = async () => {
  // 表单验证
  await loginFormRef.value.validate()
  
  loading.value = true
  try {
    // 获取员工列表
    const employeeList = await listEmployee()
    const employee = employeeList.find(
      (item) => item.employeeName === loginForm.employeeName && 
                item.password === loginForm.password && 
                item.role === 0  // 只允许角色为0（管理员）的员工登录
    )
    
    if (employee) {
      // 登录成功，保存用户信息
      userStore.setUserInfo(employee.id, employee.employeeName, 'admin')
      ElMessage.success('登录成功')
      
      // 跳转到首页或重定向页面
      const redirect = route.query.redirect || '/'
      router.push(redirect)
    } else {
      ElMessage.error('账号或密码错误，或无管理员权限')
    }
  } catch (error) {
    console.error('登录失败', error)
    ElMessage.error('登录失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 跳转到注册页
const toRegister = () => {
  router.push('/register')
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="login-title">
          <h1>连锁药房管理系统</h1>
          <p>Pharmacy Management System</p>
        </div>
      </div>
      
      <div class="login-right">
        <div class="login-form-header">
          <div class="logo-container">
            <el-icon :size="50" class="logo-icon">
              <component :is="FirstAidKit" />
            </el-icon>
          </div>
          <h2>管理员登录</h2>
          <p>请使用管理员账号登录系统</p>
        </div>
        
        <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-form">
          <el-form-item prop="employeeName">
            <el-input 
              v-model="loginForm.employeeName" 
              placeholder="请输入管理员账号"
              prefix-icon="User"
              size="large"
              autocomplete="off"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              placeholder="请输入密码" 
              prefix-icon="Lock"
              type="password"
              size="large"
              show-password
              autocomplete="off"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              :loading="loading" 
              @click="handleLogin" 
              size="large" 
              class="login-button"
            >
              登录系统
            </el-button>
          </el-form-item>
          
          <div class="login-options">
            <el-button type="text" @click="toRegister">没有账号？立即注册</el-button>
          </div>
        </el-form>
        
        <div class="login-footer">
          <p>© {{ new Date().getFullYear() }} 连锁药房管理系统</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.login-box {
  width: 900px;
  height: 500px;
  display: flex;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #fff;
}

.login-title {
  text-align: center;
}

.login-title h1 {
  font-size: 32px;
  margin-bottom: 20px;
  font-weight: 600;
}

.login-title p {
  font-size: 16px;
  opacity: 0.8;
}

.login-right {
  flex: 1;
  background-color: #fff;
  padding: 40px;
  display: flex;
  flex-direction: column;
}

.login-form-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-container {
  margin-bottom: 20px;
}

.logo-icon {
  color: #1890ff;
  background-color: #e6f7ff;
  padding: 15px;
  border-radius: 50%;
}

.login-form-header h2 {
  font-size: 24px;
  margin-bottom: 10px;
  color: #333;
}

.login-form-header p {
  color: #909399;
  font-size: 14px;
}

.login-form {
  flex: 1;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.login-options {
  margin-top: 15px;
  text-align: center;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
  color: #909399;
  font-size: 12px;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .login-box {
    width: 90%;
    flex-direction: column;
    height: auto;
  }
  
  .login-left {
    padding: 30px;
  }
  
  .login-right {
    padding: 30px;
  }
}
</style> 